var GraceJS = require("../../Grace.JS/grace.js");
import {
    apiServer
} from '../../common.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navItemsArr: [],
        navItems: ['推荐'],
        navIndex: 0,
        type: '',
        page: 1,
        limit: 10,
        showBodys: true,
        list: [],
        loadingType: 1,
        isEmpty: false,
        keyworl: '',
    },
    // 点击导航
    navChange: function (index) {
        console.log(index)
        var that = this;
        this.setData({
            navIndex: index.detail,
            page: 1,
            limit: 10,
            list: [],
            keyworl: '',
            type: that.data.navItemsArr[index.detail].id
        }, function () {
            that.getList()
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMenu()
        this.getList()
    },
    clear: function () {
        var that = this;
        this.setData({
            keyworl: '',
            page: 1,
            limit: 10,
            list: [],
            loadingType: 1,
            type: ''
        }, function () {
            that.getList()
        });
    },
    confirm: function (e) {
        var that = this;
        this.setData({
            keyworl: e.detail,
            page: 1,
            limit: 10,
            list: [],
            type: ''
        }, function () {
            that.getList()
        });
    },

    inputting: function (e) {
        this.setData({
            keyworl: e.detail
        })
    },

    // 获取书籍列表
    getList: function () {
        var that = this;
        wx.showLoading({
            title: 'Loading...',
        })
        GraceJS.get(
            apiServer + 'resume.book/index', {
                page: that.data.page,
                limit: that.data.limit,
                type: that.data.type,
                keyworl: that.data.keyworl,
            }, {},
            function (res) {
                if (res.code == 0) {
                    var nowList = GraceJS.arrayConcat(that.data.list, res.data);
                    that.setData({
                        list: nowList,
                        isEmpty: false,
                        page: that.data.page + 1,
                        loadingType: res.data.length < that.data.limit ? 3 : 1,
                    }, function () {
                        if (that.data.list.length == 0) {
                            that.setData({
                                isEmpty: true
                            })
                        }
                        wx.hideLoading();
                    })
                } else {
                    GraceJS.msg(res.message)
                }
            }
        )
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        //避免多次触发
        if (that.data.loadingType == 3) {
            return;
        }
        that.getList();
    },
    //下拉刷新事件
    onPullDownRefresh: function () {
        this.setData({
            page: 1,
            limit: 10,
            loadingType: 1,
            list: [],
            navIndex: 0,
            type: '',
            keyworl: ''
        });
        this.getList();
        // 结束下拉刷新
        wx.stopPullDownRefresh();
        msg('刷新成功');
    },

    downloadFile: function (e) {
        wx.downloadFile({
            url: 'http://example.com/somefile.pdf',
            success: function (res) {
                const filePath = res.tempFilePath
                wx.openDocument({
                    filePath: filePath,
                    success: function (res) {
                        console.log('打开文档成功')
                    }
                })
            }
        })
    },

    // 获取菜单列表
    getMenu: function () {
        var that = this;
        GraceJS.get(
            apiServer + 'resume.book/bookType', {}, {},
            function (res) {
                if (res.code == 0) {
                    that.setData({
                        navItemsArr: res.data.list,
                        navItems: res.data.arrs,
                    })
                } else {
                    GraceJS.msg(res.message)
                }
            }
        )
    },
})